<template>
  <div class="app-container">
    <div class="search-container">
      <div
        style="
          display: inline-flex;
          vertical-align: middle;
          height: 14px;
          line-height: 14px;
          color: #333;
          font-size: 14px;
          margin-bottom: 15px;
        "
      >
        <span class="pr-[50px]">工序：{{ formData.processCode ?? "--" }}</span>
        <span class="pr-[50px]"
          >生产线：{{ formData.processEquipCode ?? "--" }}</span
        >
        <span class="pr-[50px]"
          >设备编号：{{ formData.equipCode ?? "--" }}</span
        >
        <span>设备名称：{{ formData.equipName ?? "--" }}</span>
      </div>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <!-- <el-button type="primary" @click="copyAlarm()">复制模板</el-button> -->
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :maxHeight="tableHeight"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="报警编号"
          prop="alarmCode"
          min-width="100"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="报警名称"
          prop="alarmName"
          min-width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="报警等级"
          prop="alarmLevel"
          min-width="80"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("ScaAlarmLevel", scope.row.alarmLevel) }}
          </template>
        </el-table-column>
        <el-table-column
          label="报警内容"
          prop="alarmContent"
          min-width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <!-- <el-table-column
          label="触发条件"
          prop="alarmTriggerConditions"
          min-width="200"
          show-overflow-tooltip
        >
        </el-table-column> -->
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
                ><i-ep-search />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
                ><i-ep-edit
              /></el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="删除"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="deleteItem(scope.row.id)"
                ><i-ep-delete
              /></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <deviceAlarmSettings-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :processEquipCode="formData.processEquipCode"
      :equipId="formData.equipId"
      :equipCode="formData.equipCode"
      :equipName="addDialog.equipName"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <deviceAlarmSettings-select
      :visible="selectDialog.visible"
      @change-visible1="changeVisible1"
    />
  </div>
</template>

<!-- 手动挂载左侧菜单高亮 -->
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  beforeRouteEnter(to, from, next) {
    to.meta.defaultActive = "/equ/esm/deviceAlarmRule";
    if (!to.query.id) {
      to.meta.title = "报警设置";
    } else {
      to.meta.title = "报警设置";
    }
    next();
  },
});
</script>
<script setup lang="ts">
import {
  useTable,
  useDictionary,
  useComputeTableHeight,
} from "@/utils/useTable";
import request from "@/utils/request";
import { useRouter, useRoute } from "vue-router";
defineOptions({
  name: "EQUESMDEVICEALARMSETTINGS",
  inheritAttrs: false,
});

const route = useRoute();

// 请求参数
const requestApi = "/iotadmin/scaequipinfo/getAlarmByPage";

// 是否需要分页
const pagination = true;

const initParam = {
  data: { relationId: route.query.id },
};

// 初始化搜索参数数据
const searchParamInit = {
  alarmCode: "",
  alarmName: "",
  alarmLevel: "",
  relationId: "",
};

const deleteParam = {
  api: "/iotadmin/scaalarmsetting/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

// 定义tableHeight, 将计算注入dataCallBack回调中
const tableHeight = ref();
function dataCallBack(data: any) {
  setTimeout(() => {
    tableHeight.value = useComputeTableHeight();
  }, 100);
  return data;
}

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog: any = reactive({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
  equipName: "",
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/iotadmin/scaalarmsetting/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开报警设置表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.equipName = formData.value.equipName;
    addDialog.title = "修改报警设置";
    getInfo(roleObj.id);
  } else {
    addDialog.equipName = formData.value.equipName;
    addDialog.title = "新增报警设置";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}
/** 打开报警设置表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.equipName = formData.value.equipName;
  addDialog.title = "报警设置详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除该条数据?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/iotadmin/scaalarmsetting/deleteItem/${id}`,
      method: "post",
    }).then((res) => {
      ElMessage.success("删除成功！");
      search();
    });
  });
}
// // 获取设备类型下拉框
// const tableData = ref([]);
// const getAlarmTable = async () => {
//   const res = await request({
//     url: "/iotadmin/scaequipinfo/getAlarmByPage",
//     method: "post",
//     data: { relationId: route.query.id },
//   });
//   if (res.code === 200) {
//     tableData.value = res.data;
//   }
// };

// 初始化搜索参数数据
let formData: any = ref([]);

// 采集数据列表
// const tableData = ref([]);
const getEquipTypeList = async (id) => {
  const res = await request({
    url: `/iotadmin/scaequipinfo/get/${id}`,
    method: "get",
  });
  if (res.code === 200) {
    formData.value = res.data;

    formData.value.equipTypeName = `${res.data.equipTypeCode}-${res.data.equipTypeName}`;
  }
};

//点击复制选择弹窗显隐
const selectDialog = ref({
  visible: false,
});
const changeVisible1 = (val: boolean, data: any) => {
  selectDialog.value.visible = val;
  getTableList();
};

// 复制
const copyAlarm = () => {
  selectDialog.value.visible = true;
};

onMounted(() => {
  // getAlarmTable();
  if (route.query.id) {
    getEquipTypeList(route.query.id);
  }
});
</script>
